using System;
using Microsoft.Web.Management.Client;
using Microsoft.Web.Management.Server;

namespace Microsoft.Web.Management.Iis.Client
{
    /// <summary>
    /// IIS UI module that handles logging configuration.
    /// </summary>
    public sealed class LoggingModule : Module
    {
        /// <summary>
        /// Initializes the module and registers logging pages witht he control panel.
        /// </summary>
        /// <param name="serviceProvider"></param>
        /// <param name="moduleInfo"></param>
        protected override void Initialize(IServiceProvider serviceProvider, ModuleInfo moduleInfo)
        {
            // Initialize base class first
            base.Initialize(serviceProvider, moduleInfo);

            // Get the Control Panel Service
            var cpl = serviceProvider.GetService(typeof(IControlPanel)) as IControlPanel;

            // See resources.resx to change these values.
            var pageServerInfo = new ModulePageInfo(this,
                typeof(ServerLoggingPage),
                Resources.ServerLoggingUiModuleTitle,
                Resources.ServerLoggingUiModuleDescription,
                Resources.ShowRulelinesHS, Resources.ShowRulelinesHS);

            var pageSiteInfo = new ModulePageInfo(this,
                typeof(SiteLoggingPage),
                Resources.SiteLoggingUiModuleTitle,
                Resources.SiteLoggingUiModuleDescription,
                Resources.ShowRulelinesHS, Resources.ShowRulelinesHS);

            var pageCentralInfo = new ModulePageInfo(this,
                typeof(CentralLoggingPage),
                Resources.CentralLoggingUiModuleTitle,
                Resources.CentralLoggingUiModuleDescription,
                Resources.ShowRulelinesHS, Resources.ShowRulelinesHS);


            // You may wish to add additional registrations or change the following one
            // Registration shows where the page displays in the UI
            if (cpl != null)
            {
                cpl.RegisterPage(ControlPanelCategoryInfo.HealthAndDiagnostics, pageServerInfo);
                cpl.RegisterPage(ControlPanelCategoryInfo.HealthAndDiagnostics, pageSiteInfo);
                cpl.RegisterPage(pageCentralInfo);
            }
        }

        /// <summary>
        /// Determines if a page is enabled.
        /// </summary>
        /// <param name="pageInfo">A ModulePageInfo representing the page to validate.</param>
        /// <returns>true if the page is enabled, otherwise false</returns>
        /// <remarks>
        /// If the connection service path is Server and the page type is the server logging page, the value returned is true.
        /// If the connection service path is Site and the page type is the site logging page, the value returned is true.
        /// If the above two statements are false, the method returns false.
        /// </remarks>
        protected override bool IsPageEnabled(ModulePageInfo pageInfo)
        {
            // Get the IIS Client Connection service
            var conn = GetService(typeof(Connection)) as Connection;

            if (conn != null)
            {
                if (conn.ConfigurationPath.PathType == ConfigurationPathType.Server)
                {
                    return (pageInfo.PageType == typeof(ServerLoggingPage));
                }
                if (conn.ConfigurationPath.PathType == ConfigurationPathType.Site)
                {
                    return (pageInfo.PageType == typeof(SiteLoggingPage));
                }
            }
            return false;
        }
    }
}
